/**
 * 
 */
package lu.med.praxiswahl.wahlumed.client.addressbook;

import java.awt.GridLayout;

import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTable;

import lu.med.praxiswahl.wahlumed.client.utilities.ColumnWidthControler;
import lu.med.praxiswahl.wahlumed.client.utilities.UniCode;
import lu.med.praxiswahl.wahlumed.client.wclient.Main;
import lu.med.praxiswahl.wahlumed.model.beans.DoctorsTBL;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.DoctorServiceInt;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumn;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * @author PatrickPierre
 * 
 */
public class AddressBookTablePanel extends JPanel implements MouseListener,
		ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 834211952637214013L;

	private JTable bookTable;
	private AddressBookModel model;
	private Main dataManager;

	private ColumnWidthControler c;
	private boolean isColumnWidthChanged;
	private TableColumn resizedColumn;

	private int selectedRow = -1;

	public AddressBookTablePanel(Main dataManager) {
		super();
		this.dataManager = dataManager;
		this.initComponents();

	}

	public void initComponents() {

		this.setLayout(new GridLayout(0, 1));

		this.model = new AddressBookModel(dataManager);

		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane);
		this.bookTable = new JTable(model);
		bookTable.setFont(new Font("Verdana", Font.PLAIN, 11));
		bookTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		bookTable.setSelectionModel(bookTable.getSelectionModel());
		bookTable.setRowHeight(50);
		bookTable.setDefaultRenderer(Object.class, new AddressBookCellRenderer(
				model));
		scrollPane.setViewportView(bookTable);
		this.bookTable.addMouseListener(this);
		this.model.transferFromDatabase();

		resizedColumn = null;

		c = new ColumnWidthControler(Main.COLUMNWIDTH_CONFIGURATION_FILE,
				"addressbooktbl", bookTable);

		for (int i = 0; i < 4; i++) {
			resizedColumn = bookTable.getColumnModel().getColumn(i);
			resizedColumn.setPreferredWidth(c.getColumnWidth(i));
		}

		bookTable.getColumnModel().addColumnModelListener(
				new TableColumnWidthListener(this));
		bookTable.getTableHeader().addMouseListener(
				new TableHeaderMouseListener(this, c));

	}

	public boolean getColumnWidthChanged() {
		return isColumnWidthChanged;
	}

	public void setColumnWidthChanged(boolean widthChanged,
			TableColumn resizedColumn) {
		isColumnWidthChanged = widthChanged;
		this.resizedColumn = resizedColumn;
	}

	public TableColumn getResizedColumn() {
		return resizedColumn;
	}

	public JTable getBookTable() {
		return bookTable;
	}

	public void setBookTable(JTable bookTable) {
		this.bookTable = bookTable;
	}

	public AddressBookModel getModel() {
		return model;
	}

	public void setModel(AddressBookModel model) {
		this.model = model;
	}

	@Override
	public void mouseClicked(MouseEvent evt) {
		int row = -1;

		JTable table = (JTable) evt.getSource();
		if (evt.getClickCount() == 2) {
			row = table.rowAtPoint(evt.getPoint());
			Long addressId = this.model.getBookRow().get(row).getId();
			// Retrieve the Address object
			DoctorServiceInt doc = this.dataManager.getDoctorContext();
			DoctorsTBL docObj = doc.retrieveAddressById(addressId);
			AddressBookEditDialog dialog = new AddressBookEditDialog(
					this.dataManager);
			dialog.fillComponents(docObj);

		}

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent evt) {
		if (evt.isPopupTrigger()) {
			showMenu(evt);
		}

	}

	protected void showMenu(MouseEvent evt) {
		JPopupMenu menu = new JPopupMenu();
		JMenuItem item_1 = new JMenuItem("L" + UniCode.getoe() + "schen");
		item_1.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/table_row_delete.png")));
		JTable table = (JTable) evt.getSource();
		this.selectedRow = table.rowAtPoint(evt.getPoint());

		item_1.addActionListener(this);
		menu.add(item_1);
		menu.show(this, evt.getX(), evt.getY());

	}

	@Override
	public void actionPerformed(ActionEvent evt) {

		Long addressId = this.model.getBookRow().get(selectedRow).getId();
		// Retrieve the Address object
		DoctorServiceInt doc = this.dataManager.getDoctorContext();
		doc.deleteHausArtz(addressId);
		this.model.transferFromDatabase();
	}

}
